//
//  DetailSongTableViewController.m
//  老司机发车了
//
//  Created by SuperNing on 16/8/9.
//  Copyright © 2016年 斌. All rights reserved.
//

#import "DetailSongTableViewController.h"

#import "DetailListModel.h"
#import "DetailListTableViewCell.h"

#import "PlayerViewController.h"
#import "MusicManager.h"


@interface DetailSongTableViewController ()

@property (nonatomic, strong) UIImageView *imgView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation DetailSongTableViewController

- (NSMutableArray *)dataArray{
    
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addHeaderView];
    
    [self analyseData];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DetailListTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *blurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    blurView.frame = self.view.bounds;
    self.tableView.backgroundView = blurView;
    
}

- (void)analyseData{
    if (_jsonNum == 0) {
        
        NSString *urlStr = [NSString stringWithFormat:@"%@%@%@",DetailListOne_Url,_listid,DetailListTwo_Url];
        [NetWorkRequestManager requestWithType:POST urlString:urlStr parDic:@{} finish:^(NSData *data) {
            
            NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            for (NSDictionary *dict in dataDic[@"content"]) {
                DetailListModel *model = [[DetailListModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [self.dataArray addObject:model];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.tableView reloadData];
                
            });
        } finishError:^(NSError *error) {
            
        }];

    } else if(_jsonNum ==1){
        NSString *topUrl = [NSString stringWithFormat:@"%@%@%@",TopListDetailOne_Url,_type,TopListDetailTwo_Url];
        
        [NetWorkRequestManager requestWithType:POST urlString:topUrl parDic:@{} finish:^(NSData *data) {
            
            NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            for (NSDictionary *dict in dataDic[@"song_list"]) {
                DetailListModel *model = [[DetailListModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [self.dataArray addObject:model];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.tableView reloadData];
                
            });
        } finishError:^(NSError *error) {
            
        }];
    }else if(_jsonNum == 2){
        
        NSString *topUrl = [NSString stringWithFormat:@"%@%@%@",SingerSongOne_Url,_tinguid,SingerSongTwo_Url];
        
        [NetWorkRequestManager requestWithType:POST urlString:topUrl parDic:@{} finish:^(NSData *data) {
            
            NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            for (NSDictionary *dict in dataDic[@"songlist"]) {
                DetailListModel *model = [[DetailListModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [self.dataArray addObject:model];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.tableView reloadData];
                
            });
        } finishError:^(NSError *error) {
            
        }];
    }
}



- (void)addHeaderView{
    
    _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenWidth * 0.65)];
    [_imgView sd_setImageWithURL:[NSURL URLWithString:self.picUrl]];
    self.tableView.tableHeaderView = _imgView;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    DetailListModel *model = self.dataArray[indexPath.row];
    cell.songNameLabel.text = model.title;
    cell.singerLabel.text = model.author;
    cell.rowNumLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
    
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 75;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PlayerViewController *playerVC = [[PlayerViewController alloc] init];
    UINavigationController *playerNC = [[UINavigationController alloc] initWithRootViewController:playerVC];
    [MusicManager shareMusicManager].index = indexPath.row;
    [MusicManager shareMusicManager].musicArray = self.dataArray;
//    [[MusicManager shareMusicManager] loadSongInfo];
    
    
    [self.navigationController presentViewController:playerNC animated:YES completion:nil];
    
}




@end
